<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <!-- 搜索表单 -->
        <el-form
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="房号" prop="infoId">
            <el-input
              id="infoId"
              v-model="queryParams.infoId"
              placeholder="请输入房间号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="房型" prop="infoName">
            <el-select
              id="infoName"
              v-model="queryParams.infoName"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in roomTypeOptions"
                :key="item.typeId"
                :label="item.typeName"
                :value="item.typeName"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="朝向" prop="infoOrientation">
            <el-input
              id="infoOrientation"
              v-model="queryParams.infoOrientation"
              placeholder="请输入房间朝向"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="楼层" prop="infoFloor">
            <el-input
              id="infoFloor"
              v-model="queryParams.infoFloor"
              placeholder="请输入房间楼层"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索</el-button>
            <el-button
              icon="el-icon-refresh"
              size="mini"
              @click="resetQuery"
            >重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 数据操作按钮 -->
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button
              v-hasAuthority="['pos:roomInfo:add']"
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
        </el-row>

        <!-- 数据展示Table -->
        <el-table v-loading="loading" :data="dataList">
          <el-table-column prop="infoId" label="房号" width="140" />
          <el-table-column prop="infoName" label="房型" width="120" />
          <el-table-column prop="infoArea" label="面积" />
          <el-table-column prop="infoBed" label="床位数(张)" />
          <el-table-column prop="infoCount" label="可住人数" />
          <el-table-column prop="infoOrientation" label="朝向" />
          <el-table-column prop="infoFloor" label="楼层" />
          <el-table-column prop="infoStatus" label="状态">
            <template slot-scope="scope">
              {{ scope.row.infoStatus =='可用'? '已启用':'已禁用' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                v-hasAuthority="['pos:roomInfo:edit']"
                size="small"
                type="text"
                icon="el-icon-edit"
                :disabled="scope.row.infoStatus =='已停用'"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <!-- <el-button
                size="small"
                type="text"
                icon="el-icon-delete"
                :disabled="scope.row.infoStatus =='已停用'"
                @click="handleDelete(scope.row)"
              >删除</el-button> -->
              <el-button
                v-hasAuthority="['pos:roomInfo:disable']"
                size="small"
                :type="scope.row.infoStatus =='可用'? 'danger':'primary'"
                icon="el-icon-status"
                @click="handleStatus(scope.row)"
              >
                <template v-if="scope.row.infoStatus == '已停用'" #default>启用</template>
                <template v-else #default>禁用</template>
              </el-button>
            </template>
            
          </el-table-column>
        </el-table>

        <!-- 分页加载 -->
        <el-pagination
          :current-page="queryParams.pageNum"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="queryParams.pageSize"
          layout="total,sizes,prev,pager,next,jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-col>
    </el-row>

    <!-- 新增/修改对话框 默认不显示 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房间号" prop="infoId">
          <el-input v-model="form.infoId" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="房型" prop="infoName">
          <el-select
            id="infoName"
            v-model="form.infoName"
            clearable
            placeholder="请选择"
            @change="updateInput"
          >
            <el-option
              v-for="item in roomTypeOptions"
              :key="item.typeId"
              :label="item.typeName"
              :value="item.typeName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="房型ID" prop="typeId">
          <el-input
            v-model="form.typeId"
            placeholder="房型ID"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="面积" prop="infoArea">
          <el-input v-model="form.infoArea" placeholder="请输入面积" />
        </el-form-item>
        <el-form-item label="床位数" prop="infoBed">
          <el-input v-model="form.infoBed" placeholder="请输入床位数" />
        </el-form-item>
        <el-form-item label="可住人数" prop="infoCount">
          <el-input v-model="form.infoCount" placeholder="请输入可住人数" />
        </el-form-item>
        <el-form-item label="朝向" prop="infoOrientation">
          <el-input v-model="form.infoOrientation" placeholder="请输入朝向" />
        </el-form-item>
        <el-form-item label="楼层" prop="infoFloor">
          <el-input v-model="form.infoFloor" placeholder="请输入楼层" />
        </el-form-item>
        <el-form-item label="状态" prop="infoStatus">
          <el-select
            id="infoStatus"
            v-model="form.infoStatus"
            clearable
            placeholder="请选择"
          >
            <el-option label="启用" value="可用" />
            <el-option label="禁用" value="已停用" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
</style>
<script>
import {
  listAllRoomInfo,
  listRoomInfoByPage,
  delRoomInfo,
  addRoomInfo,
  getRoomInfo,
  updateRoomInfo,
  changeRoomStatus
} from '@/api/pos/roomInfo'
import { listAllRoomType } from '@/api/pos/roomType'
import { options } from 'runjs'
export default {
  name: 'RoomInfo',
  // 定义属性
  data() {
    return {
      loading: false,
      dataList: [],
      roomTypeOptions: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        infoId: null,
        typeId: null,
        infoName: null,
        typeName: null,
        infoBed: null,
        infoCount: null,
        infoArea: null,
        infoOrientation: null,
        infoFloor: null,
        infoStatus: null
      },
      // 选中数组
      // ids: [],
      // 非单个禁用
      // single: true,
      // 勾选1个或多个
      multiple: true,
      // 总条数:
      total: 0,
      // 对话框标题
      // 弹出层标题
      title: '',
      // // 对话框是否显示
      open: false,
      // 表单数据
      form: {
        infoId: null,
        typeId: null,
        infoName: null,
        typeName: null,
        infoBed: null,
        infoCount: null,
        infoArea: null,
        infoOrientation: null,
        infoFloor: null,
        infoStatus: null
      },
      // 行数据
      rows: {},
      // 表单校验规则
      rules: {
        infoId: [
          { required: true, message: '房间号不能为空', trigger: 'blur' }
        ],
        infoName: [
          { required: true, message: '房间类型不能为空', trigger: 'blur' }
        ],
        typeId: [
          {
            required: true,
            message: '房间ID不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  // 计算属性，可选
  computed: {
    
  },
  // 监控data变化，可选
  watch: {},

  // 生命周期-创建完成，可选
  created() {},
  // 生命周期-挂载完成，可选
  mounted() {
    this.getroomTypeOptions()
    this.getList()
  },

  // 方法集合
  methods: {
    getList() {
      this.loading = true
      listRoomInfoByPage(this.queryParams).then((response) => {
        this.dataList = response.data.list
        // this.total = response.data.total
        this.loading = false
      })
      // // 根据 form.infoName 的值查询一个数组内对应值的 id 号
      // const selectedRoomId = this.roomTypeOptions.find(
      //   (option) => option.typeName === this.form.infoName
      // )
      // if (selectedRoomId) {
      //   this.form.typeId = selectedRoomId.typeId
      // }
      // console.log(this.form.typeId)
      // this.queryParams.typeId =  
    },
    // 当下拉框选项变化时更新输入框的值
    updateInput() {
      const selectedRoomType = this.roomTypeOptions.find(
        (option) => option.typeName === this.form.infoName
      )
      // 如果找到了选中的房间类型对象，则更新 typeId 输入框的值
      if (selectedRoomType) {
        this.form.typeId = selectedRoomType.typeId
      }
    },
    // // 获取所有类别作为Select的Options
    getroomTypeOptions() {
      // 访问后端接口，查外键所有对象
      // 结果赋值this.roomTypeOptions
      listAllRoomType().then((response) => {
        this.roomTypeOptions = response.data
      })
    },
    // /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.pageNum = 1;
      this.getList()
      console.log(this.getList)
    },
    // /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    // // 表单初始化
    resetForm() {
      this.form = {
        infoId: null,
        typeId: null,
        infoName: null,
        infoBed: null,
        infoCount: null,
        infoArea: null,
        infoOrientation: null,
        infoFloor: null,
        infoStatus: null
      }
    },
    // // 添加数据
    handleAdd() {
      // const infoId = row.infoId;
      // console.log(infoId);
      this.resetForm()
      this.open = true
      this.title = '添加数据'
    },
    // 修改数据
    handleUpdate(row) {
      this.resetForm()
      const infoId = row.infoId
      console.log(infoId)
      // this.form = row
      getRoomInfo(infoId).then((response) => {
        this.form = response.data
      })
      this.open = true
      this.title = '修改数据'
    },

    // 修改房间状态
    /*  handleStatus(row) {
      // 判断当前状态是否为“可用”，如果是，则将状态改为“已停用”，否则将状态改为“可用”
      const newStatus = row.infoStatus === '可用' ? '已停用' : '可用'
  
      // 创建一个对象来存储要更新的信息，包括房间ID和新的状态
      const newData = {
        infoId: row.infoId,
        typeId: row.typeId,
        infoName: row.infoName,
        infoBed: row.infoBed,
        infoCount: row.infoCount,
        infoArea: row.infoArea,
        infoOrientation: row.infoOrientation,
        infoFloor: row.infoFloor, 
        infoStatus: newStatus
      }
      // 调用后端接口来更新房间信息
      updateRoomInfo(newData).then(response => {
        // 更新成功后的处理逻辑
        this.$message.success(response.msg) // 在Vue组件中显示成功消息
        // 重新获取房间列表
        this.getList()
      }).catch(error => {
        // 更新失败时的处理逻辑
        this.$message.error('更新房间状态失败：' + error.message) // 在Vue组件中显示错误消息
      })
    },  */
    handleStatus(row) {
      // 反转房间状态
      // const infoId = row.infoId // 确保获取了infoId
      row.infoStatus === '已停用' ? '可用' : '已停用'
      // 调用API来修改房间状态
      changeRoomStatus(row).then(response => {
        // 更新成功后更新界面和显示消息
        this.$message.success(response.msg) // 在Vue组件中显示成功消息
        // 重新获取房间列表
        this.getList()
      }).catch(error => {
        // 更新失败时的处理逻辑
        this.$message.error('更新房间状态失败：' + error.message) // 在Vue组件中显示错误消息
      })
    },

    // 删除数据
    handleDelete(row) {
      const infoId = row.infoId
      // console.log(infoId);
      this.$confirm('是否确认删除ID为"' + infoId + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      // .then(function () {
      //   return delroomInfo(infoId);
      // })
      // .then(() => {
      //   this.getList(); // 刷新数据
      //   this.$message.success("删除成功");
      // })
      // .catch(() => {});
    },

    /** 提交按钮 */
    submitForm: function() {
      // eslint-disable-next-line eqeqeq
      this.$refs['form'].validate((valid) => {
        if (valid){
          if (this.title === '添加数据') {
            console.log('add')
            // console.log(addRoomInfo(this.form));
            addRoomInfo(this.form).then((response) => {
              this.$message.success(response.msg)
              this.getList()
              this.cancel()
            })
          } else {
            console.log('update')
            console.log(this.form)
            // console.log(updateRoomInfo(this.form));
            updateRoomInfo(this.form).then((response) => {
              this.$message.success(response.msg)
              this.getList()
              this.cancel()
            })
          }
        }
      }) 
    },
    // 取消提交
    cancel() {
      this.open = false
      this.title = ''
      this.resetForm()
    },

    // /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    }

    // 在Vue组件的methods中
    
  }
}
</script>

